package com.mongodb.config;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.net.UnknownHostException;

@Configuration
@EnableMongoRepositories(basePackages = "com.mongodb.orders.db")
public class MongoConfig extends AbstractMongoConfiguration {
//    @Autowired
//    private Environment env;

    /**
     * 指定数据库名称
     * @return
     */
    @Override
    protected String getDatabaseName() {
        return "OrdersDB";
    }

    /**
     * 创建mongo客户端
     * @return
     * @throws UnknownHostException
     */
    @Bean
    public Mongo mongo() throws UnknownHostException {
        return new MongoClient();
//        MongoCredential credential =
//                MongoCredential.createMongoCRCredential(
//                    env.getProperty("mongo.username"),
//                        "OrdersDB",
//                        env.getProperty("mongo.password").toCharArray()
//                );
//        return new MongoClient(new ServerAddress("localhost",37017),
//                Arrays.asList(credential));
    }
}
